package com.bonc.controller;

import com.bonc.entity.Employee;
import com.bonc.service.EmployeeService;
import com.bonc.utils.Constants;
import com.bonc.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author lenovo
 * @Title
 * @Description
 * @date 2021/7/27 10:56
 */
@Api(value = "EmployeeController", description = "员工-接口层")
@RestController
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @GetMapping("/test")
    public ResultVO test() {
        log.info("springboot start.");
        try {

        } catch (Exception e) {
            // todo
            return ResultVO.fail(500, "");
        }
        return ResultVO.success("SUCCESS");
    }
    @PostMapping("/updateEmployee")
    public ResultVO updateEmployee(Employee employee) {
        return employeeService.updateEmployee(employee);
    }

    @GetMapping("/findById")
    @ApiOperation(notes = "通过id查询员工信息", value = "通过id查询员工信息", response = ResultVO.class, httpMethod = "GET")
    public ResultVO findById(@RequestParam("id") Integer id) throws Exception {
        return employeeService.findById(id);
    }

    @GetMapping("/findByPage")
    @ApiOperation(notes = "通过员工姓名分页查询员工信息", value = "通过员工姓名分页查询员工信息", response = ResultVO.class, httpMethod = "GET")
    public ResultVO findByPage(@RequestParam(value = "employeeName", required = false) String employeeName,
                               @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        Map<String, Object> paramMap = new HashMap<>(16);
        paramMap.put("employeeName", employeeName);
        paramMap.put("pageNum", pageNum);
        paramMap.put("pageSize", pageSize);
        return employeeService.findByPage(paramMap);
    }

    @PostMapping("/upload")
    public ResultVO upload(@RequestParam("file") MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        String prefix = fileName.substring(fileName.lastIndexOf("."));
        String newFileName = UUID.randomUUID() + "." + prefix;
        File newFile = new File("D:\\桌面");
        if (!newFile.exists()) {
            newFile.mkdirs();
        }
        file.transferTo(new File("D:\\桌面\\" + newFileName));
        return ResultVO.success("success");
    }

}
